/*
 * Copyright (c) 2023 fred
 * Licensed under the Apache License,Version 2.0 (the "License");
 * you may not use this file except in compliance with the License.
 * You may obtain a copy of the License at
 *
 * http://www.apache.org/licenses/LICENSE-2.0
 *
 * Unless required by applicable law or agreed to in writing, software
 * distributed under the License is distributed on an "AS IS" BASIS,
 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
 * See the License for the specific language governing permissions and
 * limitations under the License.
 *
 */
import { Element } from '../nodes/Element';
import { Node } from '../nodes/Node';
import { Elements } from './Elements';
import { Evaluator } from './Evaluator';
import { FilterResult } from './FilterResult';
import { NodeFilter } from './NodeFilter';
import hilog from '@ohos.hilog';
// import  {NodeTraversor}  from './NodeTraversor';

export class Collector {
  private constructor() {
  }

  /**
   Build a list of elements, by visiting root and every descendant of root, and testing it against the evaluator.
   @param eval Evaluator to test elements against
   @param root root of tree to descend
   @return list of matches; empty if none
   */
  public static collect(evaluator: Evaluator, root: Element): Elements {
    evaluator.reset()
    let elements = new Elements()
    root.traverse({
      head(node: Node, depth: number) {
        if (node instanceof Element) {
          let el = node as Element
          if (evaluator.matches(root, el)) {
            hilog.info(0x0000, 'testTag', '%{public}s', `Collector collect head matches`);
            elements.add(el)
          }
        }
      },
      tail(node: Node, depth: number) {
      }
    },root)
    hilog.info(0x0000, 'testTag', '%{public}s', `Collector collect return`);
    return elements
  }

  /**
   Finds the first Element that matches the Evaluator that descends from the root, and stops the query once that first
   match is found.
   @param eval Evaluator to test elements against
   @param root root of tree to descend
   @return the first match; {@code null} if none
   */
  public static findFirst(evaluator: Evaluator, root: Element): Element {
    evaluator.reset()
    let finder = new FirstFinder(evaluator)
    hilog.info(0x0000, 'testTag', '%{public}s', `selectFirst Collector`);
    return finder.find(root, root)
  }

}

export class FirstFinder implements NodeFilter {
  private _evalRoot:Element = null
  private _match:Element = null
  private readonly _eval:Evaluator

  constructor(evaluator:Evaluator) {
    this._eval = evaluator
  }

  find(root:Element,start:Element):Element {
    this._evalRoot = root
    this._match = null
    root.filterNode(this,start)
    return this._match
  }
  tail(node: Node, depth: number): FilterResult {
    return FilterResult.CONTINUE
  }

  head(node: Node, depth: number): FilterResult {
    if (node instanceof Element) {
      let el = node as Element
      hilog.info(0x0000, 'testTag', '%{public}s', `Node filterNode head ${this._eval.matches(this._evalRoot,el)}`);
      if (this._eval.matches(this._evalRoot,el)) {
        this._match = el
        return FilterResult.STOP
      }
    }
    return FilterResult.CONTINUE
  }
}
